{% extends 'sales/base.html' %}
{% load static %}
{% block breadcrumb %}
<nav aria-label="breadcrumb">
  <ol class="breadcrumb">
    <li class="breadcrumb-item"><a href="{% url 'common:api_settings' %}#open">Campaign Emails</a></li>
    <li class="breadcrumb-item active">
      {% if edit_obj %}
      Edit
      {% else %}
      Create
      {% endif %}
    </li>
  </ol>
</nav>
{% endblock %}
{%block content%}
<form id="add_form" method="POST" action="" novalidate>
  <div class="overview_form_block row marl justify-content-center">
    <div class="col-md-9">
      <div class="card">
        <div class="card-body">
          <div class="card-title text-center">
            {% if edit_obj %}
            Edit Email for Campaign
            {% else %}
            Add Email for Campaign
            {% endif %}
          </div>
          <div class="row marl no-gutters">
            <div class="col-md-12">
              <div class="filter_col col-md-12">
                <div class="form-group">
                  <label>First Name {% if form.name.field.required %}<span class="error">*</span>{% endif %}</label>
                  <input type="text" name="name" class="form-control" value="{% if edit_obj %}{{edit_obj.name}}{% endif %}">
                  <span class="error">{{ form.name.errors }}</span>
                </div>
              </div>
            </div>
            <div class="col-md-12">
              <div class="filter_col col-md-12">
                <div class="form-group">
                  <label>Last Name {% if form.last_name.field.required %}<span class="error">*</span>{% endif %}
                  </label>
                  <input type="text" name="last_name" class="form-control" value="{% if edit_obj %}{{edit_obj.last_name}}{% endif %}">
                  <span class="error">{{ form.website.errors }}</span>
                </div>
              </div>
            </div>
            <div class="col-md-12">
              <div class="filter_col col-md-12">
                <div class="form-group">
                  <label>Email {% if form.email.field.required %}<span class="error">*</span>{% endif %}
                  </label>
                  <input type="text" name="email" class="form-control" value="{% if edit_obj %}{{edit_obj.email}}{% endif %}">
                  <span class="error">{{ form.email.errors }}</span>
                </div>
              </div>
            </div>
            <div class="col-md-12">
              <div class="row marl buttons_row text-center form_btn_row">
                <button class="btn btn-default save" type="submit">Save</button>
                <a href="{% url 'common:api_settings' %}" class="btn btn-default clear"
                  id="create_lead_cancel">Cancel</a>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <input type="hidden" id="save_new_form" name="savenewform">
  </div>
</form>
{% endblock %}
{% block js_block %}

<script type="text/javascript">
  $("#add_form").submit(function (e) {
    e.preventDefault();
    $form = $(this)
    $.post("", $form.serialize(), function (response) {
      $(".error_message").remove();
      if (response.error) {
        $.each(response.errors, function (field_name, error) {
          if (field_name == '__all__') {
            error = "<span class=\"error_message\" style=color:red>" + error + "</span>"
            $("span.__all__").after(error)
          } else {
            error = "<span class=\"error_message\" style=color:red>" + error + "</span>"
            $form.find("[name=" + field_name + "]").after(error)
          }
        })
      }
      else {
        window.location = response.success_url
      }
    });
  });

</script>
{% endblock js_block %}
